package com.example.deliveryapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.deliveryapplication.entity.Client
import com.example.deliveryapplication.entity.SelectString
import com.example.deliveryapplication.sql.AppDatabase
import com.example.deliveryapplication.wiget.AddClientDialog
import kotlinx.android.synthetic.main.activity_client_set.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ClientSetActivity : AppCompatActivity(),CenterStringListAdapter.CallBack{
    private val clientDao by lazy { AppDatabase.getDatabase(this).clientDao() }
    private val planItemDao by lazy { AppDatabase.getDatabase(this).planItemDao() }
    val mList by lazy {ArrayList<SelectString>()  }
    val mAdapter by lazy { CenterStringListAdapter(this,mList,this) }
    var selectPosition=-1
    private val mViewModel by lazy { ViewModelProvider(this)[ClientSetViewModel::class.java] }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_client_set)

        mRecycleView.layoutManager=LinearLayoutManager(this)
        mRecycleView.adapter=mAdapter
        mConfirmBTN.setOnClickListener {
            val dialog=AddClientDialog(this)
            dialog.setListener(object :AddClientDialog.CallBack{
                override fun onConfirmClick(name: String) {
                    lifecycleScope.launch(Dispatchers.IO) {
                        val uuid = getNewUUid()
                        var client=Client(uuid,name)
                        clientDao.insertChart(client)
                        initData()
                    }
                }

            })
            dialog.show()
        }
        initData()
       mViewModel.result.observe(this){list->
           list.forEach {
               val selectString= SelectString()
               selectString.code=it.id
               selectString.name=it.name.handlerNull()
               mList.add(selectString)
           }
           mAdapter.notifyDataSetChanged()
       }
        mViewModel.deleteResult.observe(this){
            when(it){
                -1->{
                    showToast("该客户已经在某个计划表中被使用，请先删除计划表中的数据")
                }
                0->{
                    showToast("删除失败")
                }
                else->{
                    showToast("删除成功")
                    mAdapter.removeAt(selectPosition,mList.size)
                    mList.removeAt(selectPosition)
                }
            }
        }
        mViewModel.updateResult.observe(this){
            initData()
        }
    }



    override fun onItemClick(position: Int, data: SelectString) {

    }

    override fun onItemDelete(position: Int, data: SelectString) {
        selectPosition=position
        mViewModel.deleteModel(planItemDao,clientDao,data.code)
    }

    override fun onItemEdit(position: Int, data: SelectString) {
        val dialog=AddClientDialog(this)
        dialog.setEdit(true)
        dialog.setTitle("客户")
        dialog.setData(data)
        dialog.setListener(object :AddClientDialog.CallBack{
            override fun onConfirmClick(name: String) {
                val client=Client(data.code,name)
                mViewModel.updateClient(clientDao,client)
            }
        })
        dialog.show()
    }

    fun getData(){
        mViewModel.getAll(clientDao)

    }
    fun initData(){
        mList.clear()
        getData()
    }
}